package com.codes.controller;

import com.codes.core.common.result.Result;
import com.codes.service.BuildingSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Date 2020/6/4 10:34
 * @Created by peanut
 */
@RestController
@Api(tags = "楼栋管理")
@RequestMapping("/building")
public class BuildingCtr{

    @Autowired
    private BuildingSV buildingSV;
//    @Autowired
//    private SmsImpl sms;

//    @RequestMapping("/thread")
//    public Result getThread(){
//
//        sms.sendMessage();
//        sms.sendEmail();
//
//        return Result.success("多线程访问接口");
//    }


    @ApiOperation("楼栋分页")
    @GetMapping("/page")
    public Result getPage(@RequestParam(defaultValue = "1", required = true) int page,
                          @RequestParam(defaultValue = "10", required = true) Integer limit,
                          @RequestParam Map<String, Object> map) throws Exception {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> users =buildingSV.getListByMap(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());

    }

    @ApiOperation("添加楼栋")
    @PostMapping("/save")
    @Transactional
    public Result saveBuilding(@RequestParam Map<String,Object> map) throws Exception {

        int re = 0;
        Integer buildingId = MapUtils.getInteger(map,"buildingId");
        if(null != buildingId){
            // 修改
            re = buildingSV.updateByPrimaryKeySelective(map);
            return Result.success("楼栋修改成功",re);
        }
        try {

            re = buildingSV.insertByMap(map);
        }catch (Exception e){
            System.out.println("捕获到 service 的 异常");
        }
        return Result.success("楼栋添加成功",re);

    }

    @ApiOperation("删除楼栋信息")
    @PostMapping("/del")
    public Result delete(@RequestParam Map<String,Object> map){
        Integer id = MapUtils.getInteger(map,"id");
        if( null != id){
            int re = buildingSV.deleteByPrimaryKey(id);
            if(re >0) return Result.success("删除成功",re);
        }
        return Result.failed("删除失败");
    }



}